$(function() {
	// var account =getAccount();

	getSession();

	setTimeout(function(){
        getGoodsBillCode();
    }, 200);

	//插入
	$("#insert").click(function() {
		//非空判断
		var vcustomer = $("#customer").html();
		var vgoodsBillCode = $("#goodsBillCode").val();
		var vappealContent = $("#appealContent").val();
		if (vcustomer == null || vcustomer == "") {
			alert("客户不可为空");
			return false;
		}
		if (vappealContent == null || vappealContent == "") {
			alert("投诉内容不可为空");
			return false;
		}
		insert(vcustomer, vgoodsBillCode, vappealContent);
	});

})

function getAccount(){
	$.ajax({
		url: 'http://localhost:9000/customer/getaccount',
		type: 'get',
		dataType: 'json',
		success: function(map) {
			var account = map.account;
			return account;
		}
	})
}

function getSession() {
	$.ajax({
		url: 'http://localhost:9000/customer/complaintinformation/getsession',
		type: 'get',
		dataType: 'json',
		success: function(map) {
			var customer = map.customer;
			$("#customer").html(customer);
		}
	})
}


/**
 * 查看当前客户货运单编号
 */
function getGoodsBillCode() {

    //用户编号或姓名在服务区session获取
    $.ajax({
        url: 'http://localhost:9000/customer/complaintinformation/selectgoodsbillcode',
        type: 'get',
        dataType: 'json',
        success: function(list) {
            if (list != null && list != "") {
                var content = '';
                content += '<option  value="">请选择货运单编号</option>';
                for (var i = 0; i < list.length; i++) {
                    content += '<option  value="' + list[i] + '">' + list[i] + '</option>';
                }
                $("#goodsBillCode").html(content);
            } else {
                var content = '';
                content += '<option  value="">数据获取失败,刷新再试试吧</option>';
                $("#goodsBillCode").html(content);
            }
        }
    })
}

function insert(vcustomer, vgoodsBillCode, vappealContent) {
	$.ajax({
		url: 'http://localhost:9000/customer/complaintinformation/insert?customer=' + vcustomer + '&goodsBillCode=' +
			vgoodsBillCode + '&appealContent=' + vappealContent,
		type: 'post',
		// data: {
		// 	'customer': vcustomer,
		// 	'goodsBillCode': vgoodsBillCode,
		// 	'appealContent': vappealContent
		// },
		dataType: 'json',
		success: function(map) {
			var msg = map.msg;
			if (msg.indexOf("成功") != -1) {
				alert(msg);
				// window.location.href='http://127.0.0.1:8848/logistic/html/complaintInformation.html';
				window.location.href = 'complaint.html';
			} else {
				alert(msg + ",请重试");
			}

		}
	})

}
/**
 * 根据单号查找货运单详情并将之传至模态框
 * statical-from下的goodsBillMasterController
 */
function details() {
	var goodsBillCode = $("#goodsBillCode").val();
	if (goodsBillCode == null || goodsBillCode == "") {
		var content = '';
		content += '<thead style="text-align: center">' +
			'<tr class="success" style="text-align: center">' +
			'<th>未选择货运单号</th>' +
			'</tr>' +
			'</thead>';
		$("#details").html(content);
		return false;
	}
	$.ajax({
		// url: 'http://localhost:9000/customer/complaintinformation/goodsBillDetails?goodsBillCode=' + goodsBillCode,
		url: 'http://localhost:9000/statistical/goodsbillmaster/selectbill?goodsBillCode=' + goodsBillCode,
		type: 'get',
		dataType: 'json',
		success: function(list) {
			if (list != null && list != "") {
				var content = '';
				// for(var i=0;i<list.length;i++){
				var po = list[0];
				content += '<thead style="text-align: center">' +
					'							<tr class="success" style="text-align: center">' +
					'								<th>发货客户</th>' +
					'								<th>发货地址</th>' +
					'								<th>发货日期</th>' +
					'								<th>收货客户</th>' +
					'								<th>收货客户电话</th>' +
					'								<th>收货地址</th>' +
					'								<th>预计交货日期</th>' +
					'								<th>实际交货日期</th>' +
					'								<th>代收货款</th>' +
					'								<th>代收手续费率</th>' +
					'								<th>付款方式</th>' +
					'								<th>取货方式</th>' +
					'								<th>是否审核</th>' +
					'								<th>是否结账</th>' +
					'							</tr>' +
					'						</thead>' +
					'						<tbody>';
				content += '<tr>';
				content +=
					'<td style="width:80px;vertical-align:middle">' + po.sendGoodsCustomer + '</td>' +
					'<td style="width:80px;vertical-align:middle">' + po.sendGoodsCustomerAddr + '</td>' +
					'<td style="width:80px;vertical-align:middle">' + po.sendGoodsDate + '</td>' +
					'<td style="width:80px;vertical-align:middle">' + po.receiveGoodsCustomer + '</td>' +
					'<td style="width:80px;vertical-align:middle">' + po.receiveGoodsCustomerTel + '</td>' +
					'<td style="width:80px;vertical-align:middle">' + po.receiveGoodsCustomerAddr + '</td>' +
					'<td style="width:80px;vertical-align:middle">' + po.predeliveryDate + '</td>' +
					'<td style="width:80px;vertical-align:middle">' + po.factDealDate + '</td>' +
					'<td style="width:80px;vertical-align:middle">' + po.helpAcceptPayment + '</td>' +
					'<td style="width:80px;vertical-align:middle">' + po.acceptprocedureRate + '</td>' +
					'<td style="width:80px;vertical-align:middle">' + po.payMode + '</td>' +
					'<td style="width:80px;vertical-align:middle">' + po.fetchGoodsMode + '</td>' +
					'<td style="width:80px;vertical-align:middle">' + po.ifAudit + '</td>' +
					'<td style="width:80px;vertical-align:middle">' + po.ifSettleAccounts + '</td>';

				content += '<tbody></tr>';
				// }
				$("#details").html(content);
			} else {
				// 没有查询到数据,有单号没数据显然出了问题
				var content = '';
				content += '<thead style="text-align: center">' +
					'<tr class="success" style="text-align: center">' +
					'<th>啊哦，网络开小差了,等会儿再试试吧</th>' +
					'</tr>' +
					'</thead>';
				$("#details").html(content);
			}
		}
	})
}

/**
 * 我的投诉历史
 */
function myComplaint() {
	layui.use(['layer', 'form', 'table', 'laydate', 'element'], function() {
		var layer = layui.layer; // 获得layer模块
		var form = layui.form; // 获得form模块
		var table = layui.table; // 获得table模块
		var laydate = layui.laydate; // 获得laydate模块
		var element = layui.element; // 获得element模块（和导航、tab等有关）
		var $ = layui.$; // 调用JQuery方法时
		var laytpl = layui.laytpl;
		
		parent.layer.open({
			type: 2,
			title: '我的投诉历史',
			skin: 'layui-layer-molv',
			area: ['80%', '90%'], //宽高
			content: 'http://localhost:9000/customer/html/myComplaint.html'
		});
		
	});
}

